package staff.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {
    private static final String DEFAULT_DATE_FORMAT = "dd.MM.yyyy";
    private static String dateFormat = DEFAULT_DATE_FORMAT;

    /** Converts string to date */
    public static Date stringToDate(String dateString, String format) {
        DateFormat df = new SimpleDateFormat(format);
        try {
            return df.parse(dateString);
        } catch (ParseException ex) {
            throw new IllegalArgumentException(ex);
        }
    }

    /** Converts date to string */
    public static String datetoString(Date date, String format) {
        if (date == null) {
            return "";
        }
        DateFormat df = new SimpleDateFormat(format);
        return df.format(date);
    }

    /** Return current date format */
    public static String getDateFormat() {
        return dateFormat;
    }

    /** Set current date format */
    public static void setDateFormat(String dateFormat) {
        DateUtils.dateFormat = dateFormat;
    }

}
